﻿/*******************************************************************/
/* Author :  LongTP6B
 * Project:  OFB - Online Feedback
 * Module :  UserFeedback - FeedbackBLL Layer
 * Date   :  28 - 03 - 2013
/*******************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.ViewModel;
using OFB.BLL.Context;
using System.Data;
namespace OFB.BLL.Repository
{
    /// <summary>
    /// Feedback BLL
    /// </summary>
    public class FeedbackBLL : IFeedbackBLL
    {
        FeedbackContext context;

        /// <summary>
        /// Create Feedback context
        /// </summary>
        public FeedbackBLL()
        {
            context = new FeedbackContext();
        }

        /// <summary>
        /// Get information feedback
        /// </summary>
        /// <param name="fbDetailId"></param>
        /// <returns></returns>
        public FeedbackViewModel GetInfoFeedback(int fbDetailId, string traineeId)
        {
            return context.Fb.Init("Feedback_GetInfo", context.Fb.BuildParameter("FbDetailId", DbType.Int32, fbDetailId),
                                                       context.Fb.BuildParameter("TraineeId", DbType.String, traineeId)).Map().Get();
        }

        /// <summary>
        /// Show all question 
        /// </summary>
        /// <param name="fbDetailId"></param>
        /// <returns></returns>
        public ICollection<FeedbackAnswerViewModel> ListQuestion(int fbDetailId)
        {
            return context.FbAnswer.Init("Feedback_ListQuestion", context.FbAnswer.BuildParameter("FbDetailId", DbType.Int32, fbDetailId)).MapCollection<FeedbackAnswerViewModel>().GetCollection();
        }

        /// <summary>
        /// Save addition comment in database
        /// </summary>
        /// <param name="fbDetailId"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public bool SaveFeedbackComment(int fbDetailId, string comment)
        {
            IDataParameter rowEffectedParam = context.Fb.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.Fb.Execute("Feedback_SaveGeneralComment", context.Fb.BuildParameter("FbDetailId", DbType.Int32, fbDetailId),
                                               context.Fb.BuildParameter("Comment", DbType.String, comment),
                                               rowEffectedParam);

            return (int)rowEffectedParam.Value == 0 ? false : true;
        }

        /// <summary>
        /// Save feedback detail in database
        /// </summary>
        /// <param name="fbDetailId"></param>
        /// <param name="questionId"></param>
        /// <param name="mark"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public bool SaveFeedbackDetail(int fbDetailId, int questionId, float mark, string comment)
        {
            IDataParameter rowEffectedParam = context.FbAnswer.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.FbAnswer.Execute("Feedback_SaveAnswer", context.FbAnswer.BuildParameter("FbDetailId", DbType.Int32, fbDetailId),
                                               context.FbAnswer.BuildParameter("QuestionId", DbType.Int32, questionId),
                                               context.FbAnswer.BuildParameter("Mark", DbType.Double, mark),
                                               context.FbAnswer.BuildParameter("Comment", DbType.String, comment),
                                               rowEffectedParam);

            return (int)rowEffectedParam.Value == 0 ? false : true;
        }

        /// <summary>
        /// Check feedback finish
        /// </summary>
        /// <param name="fbDetailId"></param>
        /// <returns></returns>
        public bool IsFinished(int fbDetailId)
        {
            IDataParameter rowEffectedParam = context.FbAnswer.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.FbAnswer.Execute("Feedback_SetFinish", context.FbAnswer.BuildParameter("FbDetailId", DbType.Int32, fbDetailId),
                                               rowEffectedParam);

            return (int)rowEffectedParam.Value == 0 ? false : true;

        }

        public bool CheckId(int fbDetailId)
        {
            IDataParameter rowEffectedParam = context.FbAnswer.BuildParameter("Exist", DbType.Boolean, ParameterDirection.Output);
            context.FbAnswer.Execute("Feedback_CheckID", context.FbAnswer.BuildParameter("FbDetailId", DbType.Int32, fbDetailId),
                                               rowEffectedParam);
            return (int)rowEffectedParam.Value == 0 ? false : true;
        }

    }
}
